package com.caowj.composedemo.activity

import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.*
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.MaterialTheme.typography
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.caowj.composedemo.R

/**
 * Modifier修饰符
 * https://blog.csdn.net/vitaviva/article/details/113915565
 */
class ModifierActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            _decorated()
        }
    }
}


@Composable
fun _decorated() {
    Row(
        modifier = Modifier
            .fillMaxWidth()//填充整个父容器，match_parent
            .height(84.dp)
            .padding(8.dp)
            .border(1.dp, Color.LightGray, RoundedCornerShape(4.dp))
            .padding(8.dp)
    ) {

        _avatar( //头像部分
            modifier = Modifier
                .padding(4.dp)
                .align(Alignment.CenterVertically)
        )

        _info( //文字部分
            Modifier
                .weight(1f)
                .align(Alignment.CenterVertically)
        )

        _followBtn( //按钮
            Modifier.align(Alignment.CenterVertically)
        )
    }
}


@Composable
fun _avatar(modifier: Modifier) {
    Image(
        modifier = modifier
            .size(50.dp)
            .clip(CircleShape)
            .border(//边框1
                shape = CircleShape,
                border = BorderStroke(//BorderStroke 描边
                    width = 2.dp,
                    brush = Brush.linearGradient(//颜色渐变
                        colors = listOf(Color.Blue, Color.Cyan, Color.Green, Color.Yellow),
                        start = Offset(0f, 0f),
                        end = Offset(100f, 100f)
                    )
                )
            )
            .border(//边框2
                shape = CircleShape,
                border = BorderStroke(4.dp, SolidColor(Color.White))//SolidColor填充色
            ),
        painter = painterResource(id = R.mipmap.ic_launcher),
        contentDescription = null, // decorative
    )
}

@Composable
fun _info(modifier: Modifier) {
    Column(modifier = modifier) {
        Text(
            text = "name",
            color = Color.Black,
            maxLines = 1,
            style = TextStyle(
                fontWeight = FontWeight.Bold,
                fontSize = 16.sp,
                letterSpacing = 0.15.sp
            )
        )
        Text(
            text = "desc",
            color = Color.Black.copy(alpha = 0.75f),
            maxLines = 1,
            style = TextStyle( // here
                fontWeight = FontWeight.Normal,
                fontSize = 14.sp,
                letterSpacing = 0.25.sp
            )
        )
    }
}

@Composable
fun _followBtn(modifier: Modifier) {
    val backgroundShape: Shape = RoundedCornerShape(4.dp)//圆角
    Text(
        text = "Follow",
        style = typography.body1.copy(color = Color.White),
        textAlign = TextAlign.Center,
        modifier =
        modifier
            .width(80.dp)
            .clickable(onClick = { print("点击了按钮") })//点击
            .shadow(3.dp, shape = backgroundShape)//阴影
            .clip(backgroundShape)//裁剪
            .background(
                brush = Brush.verticalGradient(//线性渐变
                    colors = listOf(
                        Color.Red,
                        Color.Yellow,
                    ),
                    startY = 0f,
                    endY = 80f
                )
            )
            .padding(6.dp)
    )
}
